package com.accp.hotevent.service;

import com.accp.hotevent.entity.Event;
import com.accp.hotevent.dao.EventDao;
import com.accp.hotevent.req.EventListRequest;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class EventService {

    @Autowired
    EventDao eventDao;

    public PageInfo<Event> list(EventListRequest request){
        if(request.getSort() == null || request.getSort().equals("")){
            request.setSort("id");
            request.setOrder("desc");
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<Event> events = this.eventDao.list(request);
        PageInfo<Event> pageInfo = new PageInfo<>(events);
        return pageInfo;
    }


    public Event getOne(Integer id){
        return eventDao.getOne(id);
    }

    public void add(Event event){
        event.setCreateTime(new Date());
        eventDao.add(event);
    }

    public void updateSearchIndex(int eventId,int increase){
        Event event = eventDao.getOne(eventId);
        event.setSearchIndex(event.getSearchIndex()+increase);
        eventDao.updateSearchIndex(event);
    }

    public void updateApprovalStatus(Event event){
        eventDao.updateApprovalStatus(event);
    }
}
